package vip.meeet.algorithm;

import java.util.Arrays;

public class BinarySearch {

    public static void main(String[] args) {
        int[] arr = {81, 51, 88, 15, 59, 20, 1, 50, 66, 77, 4};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
        System.out.println(binarySearch(arr, 59));
    }

    public static int binarySearch(int[] arr, int value) {
        int low = 0;
        int high = arr.length - 1;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (value == arr[mid]) {
                return mid;
            }
            if (value < arr[mid]) {
                high = mid - 1;
            }
            if (value > arr[mid]) {
                low = mid + 1;
            }
        }
        return -1;
    }
}
